package com.blvtphan.tipocketclubs.gui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.blvtphan.tipocketclubs.R;
import com.blvtphan.tipocketclubs.abstracts.AbstractMapFragment;
import com.blvtphan.tipocketclubs.adapter.PlaceAvailableOfferAdapter;
import com.blvtphan.tipocketclubs.custom.CustomTextView;
import com.blvtphan.tipocketclubs.model.RewardsModel;
import com.blvtphan.tipocketclubs.util.Constants;
import com.blvtphan.tipocketclubs.util.DateTimeUtils;
import com.blvtphan.tipocketclubs.util.GeneralUtils;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class PlaceDetailsActivity extends AbstractMapFragment {

	private LinearLayout mLLDetailContent;
	private double lat, lng;
	private String placeName;
	private String placeAddress;
	private String placeLogo;
	private CustomTextView mTvPlaceName;
	private CustomTextView mTvPlaceAddress;
	private ImageView mImgPlaceLogo;
	private List<RewardsModel> lstRewardsModel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		savedInstanceState = getIntent().getExtras();
		initData(savedInstanceState);
		addDetailContent();
		moveToPlaceLocation(lat, lng);
	}

	@SuppressWarnings("unchecked")
	private void initData(Bundle bundle) {
		if (bundle != null) {
			lat = bundle.getDouble(Constants.PlaceDetailExtras.LAT);
			lng = bundle.getDouble(Constants.PlaceDetailExtras.LNG);
			placeName = bundle.getString(Constants.PlaceDetailExtras.NAME);
			placeAddress = bundle
					.getString(Constants.PlaceDetailExtras.ADDRESS);
			placeLogo = bundle.getString(Constants.PlaceDetailExtras.LOGO);
			lstRewardsModel = new ArrayList<RewardsModel>();
			try {
				lstRewardsModel = (List<RewardsModel>) GeneralUtils
						.bytes2Object(getIntent().getByteArrayExtra(
								Constants.PlaceDetailExtras.LST_PLACE_REWARDS));
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * addDetailContent
	 */
	private void addDetailContent() {
		if (lstRewardsModel != null || lstRewardsModel.size() > 0) {
			for (int i = 0; i < lstRewardsModel.size(); i++) {
				// convert date time
				String fromDate = DateTimeUtils.convertFormatDateToString(mContext, lstRewardsModel.get(i).getFromDate().substring(0, 9));
				fromDate += lstRewardsModel.get(i).getFromDate().substring(10);
				Log.i("DateTime", fromDate);
				String toDate = DateTimeUtils.convertFormatDateToString(mContext, lstRewardsModel.get(i).getToDate().substring(0, 9));
				toDate += lstRewardsModel.get(i).getToDate().substring(10);
				Log.i("DateTime", toDate);
				
				String period = getResources().getString(R.string.text_from) + " "
						+ fromDate + " "
						+ getResources().getString(R.string.text_to) + " "
						+ toDate;
				
				PlaceAvailableOfferAdapter offerDetailLayout = new PlaceAvailableOfferAdapter(
						mContext, lstRewardsModel.get(i).getTitle(), period);
				mLLDetailContent.addView(offerDetailLayout);
			}
		}

		mTvPlaceName.setText(placeName);
		mTvPlaceAddress.setText(placeAddress);
		Bitmap bitmap = MainActivity.mCache.getBitmapFromMemCache(placeLogo);
		if (bitmap != null) {
			mImgPlaceLogo.setImageBitmap(bitmap);
		}

	}

	/**
	 * move to merchant place
	 * @param lat
	 * @param lng
	 */
	private void moveToPlaceLocation(double lat, double lng) {
		LatLng coor = new LatLng(lat, lng);

		map.addMarker(new MarkerOptions()
				.position(coor)
				.title("Merchant name")
				.snippet("Very cool")
				.icon(BitmapDescriptorFactory
						.defaultMarker(BitmapDescriptorFactory.HUE_RED)));

		map.moveCamera(CameraUpdateFactory.newLatLngZoom(coor, 20));
		map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
	}

	@Override
	public int getContentViewId() {
		return R.layout.activity_place_details;
	}

	@Override
	public void initView() {
		mLLDetailContent = (LinearLayout) findViewById(R.id.ll_place_detail_content);
		mTvPlaceName = (CustomTextView) findViewById(R.id.txt_place_detail_name);
		mTvPlaceAddress = (CustomTextView) findViewById(R.id.txt_place_detail_address);
		mImgPlaceLogo = (ImageView) findViewById(R.id.img_place_detail_logo);
	}

	@Override
	public String getTitleBarTitle() {
		return mContext.getString(R.string.title_merchant_details_screen);
	}

	@Override
	public int getMapViewId() {
		return R.id.map_place;
	}

}
